package com.yanqu.road.dao.impl.activity.newyearbag;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagOpenUserLimit;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagUserTimesData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewYearBagOpenUserLimitDaoImpl extends TempDataObjectDao<NewYearBagOpenUserLimit> {


    public static String getNewYearBagOpenUserLimitDataSql = "select * from `t_c_new_year_bag_openid_userid` where `activityId`=?";
    public Map<String, NewYearBagOpenUserLimit> getNewYearBagOpenUserLimitData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, NewYearBagOpenUserLimit> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewYearBagOpenUserLimitDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewYearBagOpenUserLimit data = parse(rs);
                result.put(data.getOpenId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateNewYearBagOpenUserLimitDataSql = "update `t_c_new_year_bag_openid_userid` set xianJinTimes=?,shiWuTimes=? where activityId=? and openId=?";
    @Override
    protected boolean updateData(NewYearBagOpenUserLimit data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getXianJinTimes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getShiWuTimes())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getOpenId()));
        if (getDb().executeNoneQuery(updateNewYearBagOpenUserLimitDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewYearBagOpenUserLimitDataSql = "insert into `t_c_new_year_bag_openid_userid`(activityId,openId,xianJinTimes,shiWuTimes) values (?,?,?,?)";
    @Override
    protected boolean addData(NewYearBagOpenUserLimit data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getOpenId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getXianJinTimes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getShiWuTimes())));
        if (getDb().executeNoneQuery(insertNewYearBagOpenUserLimitDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewYearBagOpenUserLimit parseData(ResultSet rs) throws SQLException {
        NewYearBagOpenUserLimit data = new NewYearBagOpenUserLimit();
        data.setActivityId(rs.getInt("activityId"));
        data.setOpenId(rs.getString("openId"));
        data.setXianJinTimes(JSON.parseObject(rs.getString("xianJinTimes"), NewYearBagUserTimesData.class));
        data.setShiWuTimes(JSON.parseObject(rs.getString("shiWuTimes"), NewYearBagUserTimesData.class));

        return data;
    }
}
